articles

Home / DeveloperSection / Articles / MaskedTextBox Control in VB.Net

MaskedTextBox Control in VB.Net

Pushpendra Singh23857 12-Dec-2010

A MaskedTextBox control provides validation mechanism for user input on a Form. By default Masked property is set to None and the control works like a normal TextBox control.

How use MaskedTextBox Control

Drag and drop MaskedTextBox and Button (change its caption to Submit) control from toolbox on the window Form.

MaskedTextBox Control in VB.Net

Code:
PublicClass Form10
 
    PrivateSub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
        'Here TextBox is masked as number show you can enter number only
        MaskedTextBox1.Mask = "00000000000000000"
    EndSub
 
    PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Enter number will show in Label
        Label2.Text = "Entered Number is  " + MaskedTextBox1.Text
    EndSub
EndClass

 

Run The Project

MaskedTextBox Control in VB.Net

When you enter other than number then masketextbox will not accept.Because in code mask is specified as  MaskedTextBox1.Mask = "00000000000000000" which means it will accept only number.

MaskedTextBox Control in VB.Net

When you enter number and click Submit Button then click event of submit button will fire and entered number will show in the Label.

MaskedTextBox Property

UseSystemPasswordChar: Gets or sets a value indicating as password character.

PrivateSub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
        ' entered number will show as password
        MaskedTextBox1.UseSystemPasswordChar = True
EndSub


MaskedTextBox Control in VB.Net

ForeColor:

ForeColor ofMaskedTextBox can be changed through ForeColor property of MaskedTextBox.

PrivateSub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
        'change ForeColor of MaskedTextBox
        MaskedTextBox1.ForeColor = Color.Red
EndSub

ForeColor  of MaskedTextBox will bechanged.

MaskedTextBox Control in VB.Net



Updated 15-Mar-2020

Leave Comment

Comments

Liked By